package edu.gatech.cs2340.group7.wheresmystuffapp.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author tbowling3 Represents an account on the system that represents a real
 *         world user
 */
public class Account {
	private String email;
	private String password;
	private Date accountDate;
	private boolean isLocked;
	private String phone;
	private int loginAttempts;
	private long failTime;
	private boolean isAdmin;
	private static Account currentUser = null;
	private List<Item> ItemList = new ArrayList<Item>();

	/**
	 * @param email
	 *            The user's email address
	 * @param password
	 *            The user's password
	 * @param phone
	 *            The user's phone number
	 */
	public Account(String email, String password, String phone, boolean isAdmin) {
		this.email = email;
		this.password = password;
		this.phone = phone;
		this.isAdmin = isAdmin;
		this.accountDate = new Date();
		this.isLocked = false;
		this.loginAttempts = 0;
		this.failTime = -1;

	}

	// The Getters and Setters
	/**
	 * @return the current user that is logged in
	 */
	public static Account getCurrentUser() {
		return currentUser;
	}

	/**
	 * Sets the current user
	 * 
	 * @param user
	 *            the account to set the current user to
	 */
	public static void setCurrentUser(Account user) {
		currentUser = user;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Changes the password of the account
	 * 
	 * @param oldPassword
	 *            the old password
	 * @param newPassword
	 *            the new password
	 * @return true if the password was changed successfully
	 */
	public boolean changePassword(String oldPassword, String newPassword) {
		boolean out = false;
		if (password.equals(oldPassword)) {
			password = newPassword;
			out = true;
		}
		return out;
	}

	/**
	 * Checks to see if the password matches the one stored
	 * 
	 * @param attempt
	 *            the password that is going to attempt to match
	 * @return
	 */
	public boolean matchPassword(String attempt) {
		boolean out = false;
		if (attempt != null) {
			out = password.equals(attempt);
		}
		return out;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @return the accountDate
	 */
	public Date getAccountDate() {
		return accountDate;
	}

	/**
	 * @param accountDate
	 *            the accountDate to set
	 */
	public void setAccountDate(Date accountDate) {
		this.accountDate = accountDate;
	}

	/**
	 * @return the isLocked
	 */
	public boolean isLocked() {
		return isLocked;
	}

	/**
	 * @param isLocked
	 *            the isLocked to set
	 */
	public void setLocked(boolean isLocked) {
		this.isLocked = isLocked;
		setLoginAttempts(0);
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * Sets the number of times the user has tried/failed to login
	 * 
	 * @param loginAttempts
	 *            New number of times the user has tried/failed to login
	 */
	public void setLoginAttempts(int loginAttempts) {
		this.loginAttempts = loginAttempts;
	}

	/**
	 * 
	 * @return The number of times the user has tried/failed to login
	 */
	public int getLoginAttempts() {
		return this.loginAttempts;
	}

	/**
	 * Sets the amount of time the user should have had a failed login
	 * 
	 * @param l
	 *            The new fail time
	 */
	public void setFailTime(long l) {
		this.failTime = l;
	}

	/**
	 * 
	 * @return How long it has been since the user had a failed login
	 */
	public long getFailTime() {
		return this.failTime;
	}

	/**
	 * 
	 * @return Returns true if this account is an admin and false otherwise
	 */
	public boolean isAdmin() {
		return isAdmin;
	}

	/**
	 * Checks to see if two accounts are equal
	 * @param a2 The account to check for equality
	 * @return True if the accounts are the same and false otherwise
	 */
	public boolean equals(Account a2) {
		boolean out = false;
		if (a2 != null) {
			if (this.email.equals(a2.getEmail()) 
					&& a2.matchPassword(password)
					&& isAdmin == a2.isAdmin
					&& this.phone.equals(a2.getPhone())) {
				out = true;
			}
		}
		return out;
	}
	
	public long addItem(Item i){
		long out = 0;
		try{
			ItemList.add(i);
		} catch(Exception e){
			out = -1;
		}
		return out;
	}

} // close Account class
